package com.sfy.lotterysystem.controller;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CaptchaController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/captcha")
    public Map<String, String> getCaptcha() {
        // 生成验证码
        SpecCaptcha captcha = new SpecCaptcha(120, 40, 4); // 宽,高,长度
        String code = captcha.text().toLowerCase();
        String uuid = UUID.randomUUID().toString();

        // 存入Redis，5分钟有效
        redisTemplate.opsForValue().set("captcha:" + uuid, code, 5, TimeUnit.MINUTES);

        // 返回base64图片和uuid
        Map<String, String> result = new HashMap<>();
        result.put("uuid", uuid);
        result.put("img", captcha.toBase64());
        return result;
    }
}